package com.yyy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyy.common.util.PageResult;
import com.yyy.entity.Department;
import com.yyy.entity.Employee;
import com.yyy.mapper.EmployeeMapper;
import com.yyy.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyy.service.IEmployeeService;
import com.yyy.vo.EmployeeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyy
 * @since 2025-01-10
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private IDepartmentService iDepartmentService;
    @Override
    public PageResult<Employee> findByPage(Integer page, Integer pageSize) {

        Page<Employee> pg = this.page(new Page<>(page, pageSize));
        return new PageResult<>(pg.getRecords(), pg.getTotal());
    }

    @Override
    public PageResult<Employee> search(Integer page, Integer pageSize, EmployeeVO vo) {
        //条件查询
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getEname()), "ename", vo.getEname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getDid() != null, "did", vo.getDid());

        Page<Employee> pg = this.page(new Page<>(page, pageSize), wrapper);
        pg.getRecords().forEach(em ->{
            Department department = iDepartmentService.getById(em.getEid());
            em.setDname(department.getDname());
        });
        return new PageResult<>(pg.getRecords(), pg.getTotal());
    }

    @Override
    public boolean add(Employee employee) {
        employee.setDeleted(0);
        return this.save(employee);
    }

}
